package com.qf.hospital.system.controller;


import com.github.pagehelper.PageInfo;
import com.qf.hospital.system.entity.SysRole;
import com.qf.hospital.system.enums.HospitalEnum;
import com.qf.hospital.system.service.SysRoleService;
import com.qf.hospital.system.util.R;
import com.qf.hospital.system.vo.DTreeVO;
import com.qf.hospital.system.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 查询角色列表信息
     * # path&method
     *     Request URL: http://localhost:8080/view/role/role/loadAllRole
     *     Request Method: GET
     * # parameter
     * 	 rolename: String
     *     roledesc: String
     *     available: Integer
     *     page: 1
     *     limit: 10
     * # result
     * SUCCESS：
     * {
     *     "code": 0,
     *     "msg": "",
     *     "count": 111,
     *     "data": [
     *         {"roleid": 1,"rolename": "角色名称","roledesc": "角色备注","available": 0},
     *         {"roleid": 1,"rolename": "角色名称","roledesc": "角色备注","available": 0}
     *     ]
     * }
     */
    @GetMapping("/view/role/role/loadAllRole")
    public ResultVO loadAllRole(SysRole sysRole, @RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer limit){
        //1. 调用service查询
        PageInfo<SysRole> pageInfo = sysRoleService.findByConditionAndPage(sysRole, page, limit);
        //2. 返回
        return R.ok(pageInfo.getTotal(),pageInfo.getList());
    }


    /**
     * 查询角色的菜单树
     * # path&method
     *     Request URL: http://localhost:8080/view/role/role/initRoleMenuTreeJson
     *     Request Method: POST
     * # parameter
     * 	roleid=1
     * # result
     * SUCCESS:
     * {
     *     "code": 0,
     *     "msg": "",
     *     "data": [
     *                {"id": 1,"title": "阿巴","parentId": 父节点id,"spread": false,"checkArr": 复选框是否选中},
     *         {"id": 2,"title": "门诊管理","parentId": 1,"spread": false,"checkArr": 1},
     *         {"id": 3,"title": "用户挂号","parentId": 2,"spread": false,"checkArr": 1},
     *         ……
     *      ]
     * }
     */
    @PostMapping("/view/role/role/initRoleMenuTreeJson")
    public ResultVO initRoleMenuTreeJson(Integer roleid){
        //1. 参数校验
        if(roleid == null){
            log.info("【查询菜单树】 请求参数不合法, roleid = {}",roleid);
            return R.error(HospitalEnum.PARAMETER_ERROR.getCode(),"请求参数不合法…………");
        }

        //2. 调用service查询数据
        List<DTreeVO> data = sysRoleService.initRoleMenuTreeJson(roleid);

        //3. 封装返回
        return R.ok(data);
    }

}
